GeneralHID.deviceList;
GeneralHID.stopEventLoop;
~joy.close
~joy.debug( true );
~joy.makeGui;
~joy.info.findArgs;
~joy.caps;
GeneralHID.eventLoopIsRunning;

~jinfo = {
	~joy.slots.do{ | slot, x |
		"slot: ".post; x.postln;
		slot.do{ | sub, y |
			"	sub ".post;
			y.post;
			" ".post;
			sub.postln } };
	nil };

~jslots = // control slots for a logitech dual control usb controller
~joy.slots.keys.collectAs({ | key |
	[ key, ~joy.slots.at(key).collectAs({ | slot | slot.id }, Array) ] },
	Array).quickSort({|x, y| x[0] < y[0]}).collect{|x| x[1]};

~jslot = { | x, y |
	~joy.slots[x].at( ~jslots[x][y] )}

~debugj = { | x, y |
	"setting controller ".post; x.post; " ".post; y.postln;
		(~joy.slots[x]).at( y ).action_({ |v|
		"val of ".post;
		x.post;
		" at ".post;
		y.post;
		" is : ".post;
		v.value.postln })};

~ndebugj = { | x, y |
	~joy.slots[x].at( ~jslots[x][y] ).action_({});
}

~debugj.( 1, 6 );

try {
	~jslots.do{| slot, x |
		slot.do{ | idx |
			~debugj.( x, idx ) } }
} { | a, b, c, d, e | [ "error", a, b, c, d, e ].postln
}
~debugj.( 2, 0 )

~joyous.fronts[0].action_{ | v | ~ctl.set( v.value / 127 ) }

~ctl = Bus.control


SynthDef(\mybutt, {
  arg freq = 0.5, amp= 0.5, carr= 0.48, trig;
  var randy = Dwhite(0, 2000, inf);
  var du;
  // trig = Dust.kr(30, 5);
  du  = Demand.kr(trig, 0, randy);
  // Out.ar([0,1], SinOsc.ar(du));
  Out.ar( [0 , 1],
	PMOsc.ar(
	  [ \freq.asSpec.map( freq ),
		\freq.asSpec.map( freq ) *1.0001 ],
	  du * carr * 18473, 3, 0, amp));
}).send(s);

~syn = Synth(\mybutt);
~syn.free;

~syn.map(\trig, ~ctl);
~ctl.set( 3 );
~ctl.set( 0 );

SynthDef(\yourbutt, {
  arg trig = 1, seq, carr=1, amp=0.5, freq=0.5;
  //trig = Impulse.kr(8);
  seq = Dwhite(0, 2000, inf);
  trig = Demand.kr(trig, 0, seq) * trig;
  Out.ar( [ 0, 1 ],
	PMOsc.ar(
	  [ \freq.asSpec.map( freq ),
		\freq.asSpec.map( freq ) * 1.0001 ],
	  trig * carr * 18473, 3, 0, amp ) );
}).send(s)

~syn = Synth(\yourbutt);
